package gui;

import hcs.interfaces.StateObserver;
import hcs.model.Boiler;
import hcs.model.Environment;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Observable;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 * <p>
 * An EnvironmentMonitor displays information retrieved from
 * the Environment.
 * This component is named "EnvironmentMonitor"
 * </p>
 * <p> The temperature label is named EnvironmentMonitor#temperature</p>
 * <p> The rain label is named EnvironmentMonitor#rain</p>
 * <p> The boiler label is named EnvironmentMonitor#boiler</p>
 * <p> The boiler target label is named EnvironmentMonitor#boiler</p>
 * @author vincenzo.delia@polito.it
 *
 */
@SuppressWarnings("serial")
public class EnvironmentMonitor extends JPanel implements StateObserver{
	private JLabel tempLabel;
	private JLabel rainLabel;
	private JLabel boilerLabel;
	private JLabel boilerTargetLabel;
	
	public EnvironmentMonitor(MainFrame parent, Environment env) {
		super();
		this.setName("EnvironmentMonitor");
		env.addObserver(this);
		GridBagLayout gB = new GridBagLayout();
		GridBagConstraints lC = new GridBagConstraints();
		lC.weightx = 1.0;
		lC.gridwidth = GridBagConstraints.REMAINDER;
		this.setLayout(gB);
		GuiUtilities.fitNewLabel(this,gB,lC,"Environment", "Helvetica", Font.BOLD, 15);
		this.tempLabel = GuiUtilities.fitNewLabel(this,gB,lC,"Temperature: -");
		this.tempLabel.setName("EnvironmentMonitor#temperature");
		this.rainLabel = GuiUtilities.fitNewLabel(this,gB,lC,"Rain: -");
		this.rainLabel.setName("EnvironmentMonitor#rain");
		this.boilerLabel = GuiUtilities.fitNewLabel(this,gB,lC,"Boiler: -");
		this.boilerLabel.setName("EnvironmentMonitor#boiler");
		this.boilerTargetLabel = GuiUtilities.fitNewLabel(this,gB,lC,"Boiler Target: -");
		this.boilerTargetLabel.setName("EnvironmentMonitor#boiler-target");
		this.setBackground(Color.WHITE);
		this.setBorder(new LineBorder(new Color(211,211,220), 3));
		parent.getHouseController().addBoilerObserver(this);
	}

	public void update(Observable o, Object arg) {
		if(o instanceof Environment) {
			Environment e = (Environment)o;
			double t = e.getTemperature();
			boolean rain = e.isRaining();
			tempLabel.setText(String.format("Temperature: %.1f", t));
			rainLabel.setText("Rain: " + rain);
		}
		if (o instanceof Boiler) {
			Boiler boiler = (Boiler)o;
			String current = String.format("%.1f", boiler.getCurrentTemperature());
			String target = String.format("%.1f", boiler.getTargetTemperature());
			boilerLabel.setText("Boiler: "+ current);
			boilerTargetLabel.setText("Boiler Target: "+ target);
			if(!target.equals(current)) {
				boilerLabel.setForeground(Color.RED);
			} else
				boilerLabel.setForeground(Color.BLACK);
			
		}
	}
	
}
